package wallet

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
	"navi-mumbai/common/key/dbkey"
)

var _ CurrencyExchangeConfigModel = (*customCurrencyExchangeConfigModel)(nil)

type (
	// CurrencyExchangeConfigModel is an interface to be customized, add more methods here,
	// and implement the added methods in customCurrencyExchangeConfigModel.
	CurrencyExchangeConfigModel interface {
		currencyExchangeConfigModel
		Find(ctx context.Context, state, payChannelId, pageNo, pageSize int64) ([]*CurrencyExchangeConfig, error)
		Count(ctx context.Context, state int64) (int64, error)
		FindConfig(ctx context.Context, moneyType, moneyAmount, currencyType, currencyAmount, payChannelId int64) (*CurrencyExchangeConfig, error)
	}

	customCurrencyExchangeConfigModel struct {
		*defaultCurrencyExchangeConfigModel
		genSql *gensql.GenSql
	}
)

// NewCurrencyExchangeConfigModel returns a model for the database table.
func NewCurrencyExchangeConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) CurrencyExchangeConfigModel {
	return &customCurrencyExchangeConfigModel{
		defaultCurrencyExchangeConfigModel: newCurrencyExchangeConfigModel(conn, c, opts...),
		genSql:                             gensql.NewGenSql(),
	}
}

func (m *customCurrencyExchangeConfigModel) Find(ctx context.Context, state, payChannelId, pageNo, pageSize int64) ([]*CurrencyExchangeConfig, error) {
	// 分页
	if pageNo < 1 {
		pageNo = 1
	}
	if pageSize <= 0 {
		pageSize = 10
	}

	q := fmt.Sprintf("select %s from %s ", currencyExchangeConfigRows, m.table)
	m.genSql.InitSql(q)

	if payChannelId != 0 {
		m.genSql = m.genSql.Where("pay_channel_id = ?", payChannelId)
	}
	if state != 0 {
		m.genSql = m.genSql.Where("state = ?", state)
	}
	query, values, err := m.genSql.OrderBy("create_time ASC").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize)).ToSql()
	if err != nil {
		return nil, err
	}

	resp := make([]*CurrencyExchangeConfig, 0)
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

func (m *customCurrencyExchangeConfigModel) Count(ctx context.Context, state int64) (int64, error) {
	q := fmt.Sprintf("select count(id) from %s ", m.table)
	m.genSql.InitSql(q)

	if state != 0 {
		m.genSql = m.genSql.Where("state = ?", state)
	}
	query, values, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}

	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}

func (m *customCurrencyExchangeConfigModel) FindConfig(ctx context.Context, moneyType, moneyAmount, currencyType, currencyAmount, payChannelId int64) (*CurrencyExchangeConfig, error) {
	q := fmt.Sprintf("select %s from %s ", currencyExchangeConfigRows, m.table)
	m.genSql.InitSql(q)
	if moneyType != 0 {
		m.genSql = m.genSql.Where("money_type = ?", moneyType)
	}
	if moneyAmount != 0 {
		m.genSql = m.genSql.Where("money_amount = ?", moneyAmount)
	}
	if currencyType != 0 {
		m.genSql = m.genSql.Where("currency_type = ?", currencyType)
	}
	if currencyAmount != 0 {
		m.genSql = m.genSql.Where("currency_amount = ?", currencyAmount)
	}
	if payChannelId != 0 {
		m.genSql = m.genSql.Where("pay_channel_id = ?", payChannelId)
	}

	q, args, err := m.genSql.Where("state = ?", dbkey.Enable).Limit(1).ToSql()
	if err != nil {
		return nil, err
	}
	var resp CurrencyExchangeConfig
	err = m.QueryRowNoCacheCtx(ctx, &resp, q, args...)
	switch err {
	case nil:
		return &resp, nil
	default:
		return nil, err
	}
}
